home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / lock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  1.5 KB  |  81 lines

  1. #include "lib.h"
  2.  
  3. #include <errno.h>
  4. #include <fcntl.h>
  5.  
  6. #define LOCKDIR "/tmp/"     /* or /usr/tmp/ as the case may be */
  7. #define MAXTRIES 3
  8. #define NAPTIME 5
  9. #ifdef ATARI_ST
  10. #define syserr(X)    /* just check errno */
  11. #ifdef TRUE
  12. #undef TRUE
  13. #endif
  14. #ifdef FALSE
  15. #undef FALSE
  16. #endif
  17. #define TRUE 1
  18. #define FALSE (~TRUE)
  19. #define BOOLEAN int
  20. #else
  21. /* This is bogus shit unless we have a <lock.h> or something */
  22. typedef enum { FALSE, TRUE } BOOLEAN;
  23. #endif
  24.  
  25. #ifdef __STDC__
  26. static char *lockpath(_CONST char *);
  27. #else
  28. static char *lockpath();
  29. #endif
  30.  
  31. BOOLEAN lock(name)          /* acquire lock */
  32. _CONST char *name;
  33. {
  34.     char *path;
  35. #ifndef __STDC__
  36.     char *lockpath();
  37. #endif
  38.     int fd, tries;
  39.     extern int errno;
  40.  
  41.     path = lockpath(name);
  42.     tries = 0;
  43.  
  44.     /* this will work because FS is single threaded, if that is changed
  45.        then it is about as useless  */
  46.     while ((fd = open(path, O_WRONLY| O_CREAT | O_EXCL, 0600)) == -1 && errno == EACCES)
  47.  
  48.     {
  49.         if (++tries >= MAXTRIES)
  50.             return(FALSE);
  51.         sleep(NAPTIME);
  52.     }
  53.     if (fd == -1 || close(fd) == -1)
  54.         syserr("lock");
  55.     return(TRUE);
  56. }
  57.  
  58. void unlock(name)           /* free lock */
  59. _CONST char *name;
  60. {
  61. #ifndef __STDC__
  62.     char *lockpath();
  63. #endif
  64.  
  65.     if (unlink(lockpath(name)) == -1)
  66.         syserr("unlock");
  67. }
  68.  
  69. static char *lockpath(name) /* generate lock file path */
  70. _CONST char *name;
  71. {
  72.     static char path[20];
  73. #ifndef __STDC__    /* for __STDC__ pick up proto from <std.h> */
  74.     char *strcat();
  75. #endif
  76.  
  77.     strcpy(path, LOCKDIR);
  78.     return(strcat(path, name));
  79. }
  80.  
  81.